package com.example.mobile2032021606170hsy0511;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    final  String TEXTPATH = "http://192.168.43.18:3000/web/text.txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL textUrl = new URL(TEXTPATH);
                    HttpURLConnection textConnection=(HttpURLConnection)textUrl.openConnection();
                    InputStream textIS=textConnection.getErrorStream();
                    InputStreamReader isr=new InputStreamReader(textIS);
                     BufferedReader br =new BufferedReader(isr);
                    String line=null;
                    String temp="";
                    while ( (line=br.readLine())!=null){
                        temp+=line;
                        temp +="/n";
                    }
                    final  String msg=temp;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvMainTitle.setText(msg);
                        }
                    });
                    br.close();
                    isr.close();
                    textIS.close();
                    textConnection.disconnect();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
    }
}